// Pavilion MCP Coordinator - Main client that coordinates between servers and agents
// Pavilion MCP协调器 - 在服务器和Agent之间协调的主要客户端
const MCPClient = require('./MCPClient');
const WaterQualityMCPServer = require('../mcp-server/WaterQualityMCPServer');

class PavilionMCPClient extends MCPClient {
  constructor() {
    super('PavilionCoordinator');
    this.agents = new Map();
  }

  // Register an agent
  // 注册一个Agent
  registerAgent(agentName, agent) {
    this.agents.set(agentName, agent);
  }

  // Initialize with servers
  // 使用服务器进行初始化
  initialize() {
    super.initialize();
    
    // Connect to water quality server
    // 连接到水质服务器
    const waterQualityServer = new WaterQualityMCPServer(3001);
    waterQualityServer.start();
    this.connectToServer('waterQualityServer', waterQualityServer);
    
    console.log('Pavilion MCP Coordinator connected to servers');
    console.log('Pavilion MCP协调器已连接到服务器');
  }

  // Run coordination logic
  // 运行协调逻辑
  run() {
    console.log('Pavilion MCP Coordinator running');
    console.log('Pavilion MCP协调器运行中');
    
    // Periodically check data and coordinate agents
    // 定期检查数据并协调Agent
    setInterval(() => {
      // Get water quality data
      // 获取水质数据
      const waterQuality = this.getDataFromServer('waterQualityServer', 'waterQuality');
      if (waterQuality) {
        console.log('Current water quality:', waterQuality);
        console.log('当前水质:', waterQuality);
        
        // Notify water quality monitoring agent
        // 通知水质监测Agent
        const waterQualityAgent = this.agents.get('waterQualityAgent');
        if (waterQualityAgent) {
          waterQualityAgent.updateData(waterQuality);
        }
      }
      
      // Get chemical dosage data
      // 获取化学加药数据
      const chemicalDosage = this.getDataFromServer('waterQualityServer', 'chemicalDosage');
      if (chemicalDosage) {
        console.log('Current chemical dosage:', chemicalDosage);
        console.log('当前化学加药情况:', chemicalDosage);
        
        // Notify chemical dosage control agent
        // 通知化学加药控制Agent
        const chemicalDosageAgent = this.agents.get('chemicalDosageAgent');
        if (chemicalDosageAgent) {
          chemicalDosageAgent.updateData(chemicalDosage);
        }
      }
    }, 5000);
  }
}

module.exports = PavilionMCPClient;